package com.bestom.bluetoothlock.bluetooth;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.util.LogMsg;
import com.bestom.bluetoothlock.util.LogUtils;

import org.apache.commons.codec.binary.Hex;

public class BleServiceManage {
    private static final String TAG = "BleServiceManage";
    private static BleServiceManage instance;
    private BleConnection bleConnection;
    private Context context;
    public BlueService service;

    public final class BleConnection implements ServiceConnection {
        public BleConnection() {
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            BleServiceManage.this.service = ((BlueService.LocalBinder) iBinder).getService();
            LogMsg.Mess("绑定蓝牙服务成功" + BleServiceManage.this.service);
        }

        public void onServiceDisconnected(ComponentName componentName) {
            LogMsg.Mess("绑定蓝牙服务失败");
            BleServiceManage.this.service = null;
        }
    }

    public static BleServiceManage getInstance(Context context2) {
        if (instance == null) {
            synchronized (BleServiceManage.class) {
                if (instance == null) {
                    instance = new BleServiceManage(context2);
                }
            }
        }
        return instance;
    }

    private BleServiceManage(Context context2) {
        this.context = context2;
    }

    public void init() {
        initBleService(this.context);
    }

    private void initBleService(Context context2) {
        try {
            if (this.bleConnection == null) {
                this.bleConnection = new BleConnection();
            }
            context2.bindService(new Intent(context2, BlueService.class), this.bleConnection, Context.BIND_AUTO_CREATE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void connect(String str) {
        if (str == null) {
            LogUtils.i(TAG, "address null");
        }
        if (this.service == null) {
            initBleService(this.context);
        }
        Log.d("agui", "connect " + str);
        this.service.connect(str);
    }

    public void setRegistered(Boolean bool) {
        this.service.isRegistered = bool.booleanValue();
    }

    public void setPassword(String str) {
        BlueService blueService = this.service;
        blueService.password = str;
        if (blueService.password != null && this.service.password.length() == 6) {
            this.service.password_bytes[0] = (byte) Integer.parseInt(str.substring(0, 2), 16);
            this.service.password_bytes[1] = (byte) Integer.parseInt(str.substring(2, 4), 16);
            this.service.password_bytes[2] = (byte) Integer.parseInt(str.substring(4, 6), 16);
            Log.d("agui", String.format("password:%02x%02x%02x", new Object[]{Byte.valueOf(this.service.password_bytes[0]), Byte.valueOf(this.service.password_bytes[1]), Byte.valueOf(this.service.password_bytes[2])}));
        }
    }

    public void connect(DeviceInfo deviceInfo) {
        if (deviceInfo == null) {
            LogUtils.i(TAG, "device null");
        }
        if (this.service == null) {
            initBleService(this.context);
        }
        this.service.connect(deviceInfo);
    }

    public void disConnect() {
        if (this.service == null) {
            initBleService(this.context);
        }
        this.service.disconnect();
    }

    public boolean checkBLEenable() {
        BlueService blueService = this.service;
        if (blueService == null) {
            return false;
        }
        return blueService.checkBLEenable();
    }

    public void setHandlerListener(Handler handler) {
        if (this.service == null) {
            initBleService(this.context);
        }
        this.service.setHandlerListener(handler);
    }

    public void stopService() {
        BlueService blueService = this.service;
        if (blueService != null) {
            blueService.disconnect();
        }
    }

    public boolean sendData(String str) {
        if (this.service == null) {
            initBleService(this.context);
        }
        try {
            this.service.write(Hex.decodeHex(str.toCharArray()));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void startScan() {
        if (this.service == null) {
            initBleService(this.context);
        }
        this.service.scanLeDevice(true);
    }

    public void stopScan() {
        if (this.service == null) {
            initBleService(this.context);
        }
        this.service.scanLeDevice(false);
    }

    public boolean isConnect() {
        BlueService blueService = this.service;
        if (blueService == null) {
            return false;
        }
        return blueService.isConnented();
    }

    public void onDestroy() {
        stopService();
    }
}
